import React from 'react';
import { observer, inject } from 'mobx-react';
import { getAntd,getEcCom,getLabel } from '../../util/convert';
import { buildUrlStr,getBrowserVersion } from '../../util/formUtil';
import * as API_REQ from '../../apis/req';
import '../../style/printReq.less';

@inject("printReqStore")
@observer
export default class PrintReq extends React.Component{
    componentDidMount() {
        const { printReqStore,location={} } = this.props;
        printReqStore.loadTemplates(location.query);
    }
    render(){
        const { Button } = getAntd();
        const { printReqStore } = this.props;
        const { needChoose,urlParams,templates,templateKey } = printReqStore;
        return (
            <div className="wf-printReq">
                <div className="wf-printReq-left">
                    <div className="wf-printReq-left-top">
                        <span>{getLabel(386164,'请选择打印模板')}:</span>
                    </div>
                    <div className="wf-printReq-left-items">
                        {
                            templates.map(item =>{
                                return (
                                    <div className="wf-printReq-left-item">
                                        <input type="radio" name="templateKey" checked={templateKey === item.key} onClick={this.chooseTemplate.bind(this,item.key)} />
                                        <span>{item.name}</span>
                                    </div>
                                )
                            })
                        }
                    </div>
                    <div className="wf-printReq-left-bottom">
                        <Button ecId={`${this && this.props && this.props.ecId || ''}_Button@qxudwp`} type='primary' onClick={this.doConfirm}>{getLabel(83446,'确定')}</Button>
                        <Button ecId={`${this && this.props && this.props.ecId || ''}_Button@grimu0`} type='default' onClick={this.doCancel}>{getLabel(201,'取消')}</Button>
                    </div>
                </div>
                <div className="wf-printReq-right">
                    <iframe id="reqIframe" src={this.buildIframeSrc(true)} style={{border:0,width:'100%',height:'100%'}} />
                </div>
            </div>
        )
    }
    buildIframeSrc(isPreView=false){
        const { printReqStore } = this.props;
        const { urlParams,templateKey } = printReqStore;
        let iframeSrc = "about:blank";
        if(templateKey !== ""){
            const arr = templateKey.split("_");
            let iframeParams = {...urlParams, ...{ismode:arr[0], modeid:arr[1], isPreView:isPreView}};
            iframeSrc = `/workflow/request/PrintRequestSPA.jsp?${buildUrlStr(iframeParams)}`;
        }
        return iframeSrc;
    }
    chooseTemplate(key){
        const { printReqStore } = this.props;
        printReqStore.chooseTemplate(key);
    }
    doConfirm = () => {
        const browserVersion = getBrowserVersion();
        const { printReqStore } = this.props;
        const { templateKey,urlParams } = printReqStore;
        const ismode = templateKey && templateKey.split("_")[0] || "";
        if(browserVersion === "Chrome" && parseInt(ismode) !== 1){
            const reqIframe = document.getElementById("reqIframe");
            if(reqIframe.contentWindow.WfForm){
                reqIframe.contentWindow.WfForm.changeFieldValue("field-9",{"value":parseInt(reqIframe.contentWindow.WfForm.getFieldValue("field-9"))+1});//代码块对页面打印次数加1，保证打印次数一致
            }
			//未弹出页面需生成打印日志
            API_REQ.generatePrintLog(urlParams);
            reqIframe.focus();
            reqIframe.contentWindow.print();
        }else{
            const printUrl = this.buildIframeSrc();
            window.location.href = printUrl;
        }
    }
    doCancel = () => {
        try{
            window.close();
        }catch(e){}
    }
}
